/** 系统应用、资源管理器
 * 1. 系统应用
 * 2. 已打开应用窗口
 * 2. 活动窗口索引
 * 4. 资源管理器
 * */
const app = {
  state (){
    return {
      // 桌面快捷方式
      desktopLinks: [0],
      // 系统应用
      appList: [ // 系统中的应用
        {icon: '/static/image/system/icon/huatu.png', title: '画图', type: 'drawbox', noRepeatOpen: false},
        // 网络监视器；资源占用；性能
        {icon: '/static/image/system/icon/huatu.png', title: '任务管理器', type: '', noRepeatOpen: false},
        {icon: '/static/image/system/icon/icon-demo.png', title: '屏幕截图', type: ''},
        {icon: '/static/image/system/icon/icon-demo.png', title: 'mdEditor', type: ''},
        {icon: '/static/image/system/icon/icon-demo.png', title: '计算器', type: ''},
        {icon: '/static/image/system/icon/icon-demo.png', title: '放大镜', type: ''},
        {icon: '/static/image/system/icon/icon-demo.png', title: '记事本', type: ''},
        // 媒体
        {icon: '/static/image/system/icon/icon-demo.png', title: '音乐播放器', type: ''},
        {icon: '/static/image/system/icon/icon-demo.png', title: '图片查看器', type: ''},
        // 工具收藏类
        {icon: '/static/image/system/icon/icon-demo.png', title: '站长工具', type: ''},
        {icon: '/static/image/system/icon/icon-demo.png', title: '网址收藏夹', type: ''},
        // 服务器支持
        {icon: '/static/image/system/icon/icon-demo.png', title: '指数检测', type: ''},
        {icon: '/static/image/system/icon/icon-demo.png', title: '聊天室', type: ''},
        {icon: '/static/image/system/icon/icon-demo.png', title: '远程', type: ''},
      ],
      // 已打开应用列表
      openAppList: [],
      // 活动窗口索引
      activeAppIndex: -1,
    }
  },
  mutations: {
    // 打开应用
    openApp: (state, option) => {
      // 窗口容器配置
      option = Object.assign({
          pid: new Date().getTime(),
          winOption: {
            top: 250, left: 0, width: 800, height: 400, zIndex: 100 + state.openAppList.length,
            minHeight: 300, minWidth: 300,
          },
          show: true, // 是否显示 false最小化
          windowSizeType: 'initial', // 窗口尺寸类型：正常initial， 最大化 max
        },
        option);
      // 添加到列表
      state.openAppList.push(option);
      // 激活活动状态
      state.activeAppIndex = state.openAppList.length-1;
    },
    // 最小化/还原
    hideOrShowApp: (state, {index, show}) => {
      state.openAppList[index].show = show;
      state.openAppList = state.openAppList.map(item => item);
    },
    // 修改窗口尺寸、定位
    resizeAppWindow: (state, {index, winOption}) => {
      state.openAppList[index].winOption = Object.assign({}, state.openAppList[index].winOption, winOption);
      state.openAppList = state.openAppList.map(item => item);
    },
    // 关闭应用
    closeApp: (state, index) => {
      state.openAppList.splice(index, 1);
      state.activeAppIndex = state.openAppList.length-1;
    },
    // 基本操作
    openAppList: (state, data) => {
      state.openAppList = data;
      state.activeAppIndex = state.openAppList.length-1;
    },
    activeAppIndex: (state, index) => {
      state.openAppList = state.openAppList.map((item, i) => {
        if(index === i) {
          item.winOption.zIndex = 200;
        } else {
          item.winOption.zIndex = 100 + state.openAppList.length;
        }
        return item;
      });
      state.activeAppIndex = index;
    },
  },
  getters: {
    desktopLinks: (state) => {
      if(state.desktopLinks.length == 0) {
        return state.appList;
      } else {
        return state.desktopLinks.map(item => state.appList[item]);
      }

    },
    openAppList: (state) => {
      return state.openAppList;
    },
    activeAppIndex: (state) => {
      return state.activeAppIndex;
    },
  }
};
export default app;